/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sessionBeans;

import entityBeans.ModeratorEntity;
import entityBeans.ReplyEntity;
import entityBeans.SubjectEntity;
import entityBeans.ThreadEntity;
import entityBeans.UserEntity;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import utilities.HashFunctions;

/**
 *
 * @author h08182
 */

//Remplissage de la base de donnée

@Singleton
@Startup
public class initSingleton {
    @EJB
    private SubjectSessionBeanLocal subjectSessionBean;
    @PersistenceContext(unitName="forum-ejbPU")
    protected EntityManager em;
    
    @PostConstruct
     public void initialisation() {
        System.out.println("Initialisation");
        
        /* ---------------- USER PART --------------------*/
        UserEntity newUser = new UserEntity();
        newUser.setEmail("testuser@gmail.com");
        newUser.setPassword(HashFunctions.stringToMD5("p"));
        newUser.setPseudo("Ella Padkulott");
        newUser.setName("Padkulott");
        newUser.setFirstName("Ella");
        newUser.setStatus("banned");
        newUser.setSignature("J'aime les bonbons");
        em.persist(newUser);
        
        /* ---------------- MODO PART --------------------*/
        ModeratorEntity newModo = new ModeratorEntity();
        newModo.setEmail("toto@gmail.com");
        newModo.setPassword(HashFunctions.stringToMD5("p"));
        newModo.setPseudo("Padkulott");
        newModo.setName("Padkulott");
        newModo.setFirstName("Ella");
        newModo.setStatus("moderator");
        em.persist(newModo);
        
        
        /* ---------------- SUBJECT PART --------------------*/
        SubjectEntity newSubject = new SubjectEntity();
        newSubject.setTitle("Racine");
        subjectSessionBean.addSubject(null,newSubject);
        
        SubjectEntity newSubject2 = new SubjectEntity();
        newSubject2.setTitle("Gastronomie");
        subjectSessionBean.addSubject(newSubject,newSubject2);
        
        
        SubjectEntity newSubject3 = new SubjectEntity();
        newSubject3.setTitle("Sport");
        subjectSessionBean.addSubject(newSubject,newSubject3);
        
        SubjectEntity newSubject4 = new SubjectEntity();
        newSubject4.setTitle("Les chocapics");
        subjectSessionBean.addSubject(newSubject2,newSubject4);
        
        
        /* ---------------- THREAD PART --------------------*/
        ThreadEntity newThread = new ThreadEntity();
        newThread.setTitle("Et paf, ça fait des chocapics !");
        newThread.setMyAuthor(newUser);
        newThread.setMySubject(newSubject4);
        ArrayList<ThreadEntity> mysubthreadList1 = new ArrayList<ThreadEntity>();
        mysubthreadList1.add(newThread);
        newSubject4.setMyThreads(mysubthreadList1);
        
        ThreadEntity newThread2 = new ThreadEntity();
        newThread2.setTitle("Geeker est-il un sport ?");
        newThread2.setMyAuthor(newUser);
        newThread2.setMySubject(newSubject3);
        ArrayList<ThreadEntity> mysubthreadList2 = new ArrayList<ThreadEntity>();
        mysubthreadList2.add(newThread2);
        newSubject3.setMyThreads(mysubthreadList2);
        
        
        em.persist(newThread);
        newThread.setUuid(HashFunctions.stringToSHA1(newThread.getId().toString()));
        em.persist(newThread2);
        newThread2.setUuid(HashFunctions.stringToSHA1(newThread2.getId().toString()));
        /* ---------------- REPLY PART --------------------*/
        ReplyEntity newReply = new ReplyEntity();
        newReply.setContent("Voici une reponse test !");
        newReply.setMyThread(newThread);
        newReply.setMyAuthor(newUser);
        newReply.setDate(null);
        
        em.persist(newReply);
        
        System.out.println("End Of Initialisation");
     }
}
